package com.hskj.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hskj.dao.CategoryDao;
import com.hskj.dao.ProductDao;
import com.hskj.info.PageInfo;
import com.hskj.localUtil.LocalPageUtil;
import com.hskj.model.Category;
import com.hskj.model.CategoryQuery;
import com.hskj.model.Product;
import com.hskj.model.ProductQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {
    @Autowired
    private ProductDao productDao;

    public List<Product> findAll(){
        return productDao.findAll();
    }

    public List<Product> select(ProductQuery query){
        if(query.getPageNum()!=null && query.getPageSize()!= null){
            PageHelper.startPage(query.getPageNum(),query.getPageSize());
        }

        List<Product> select = productDao.select(query);
        if(select instanceof Page){
            Page page=(Page) select;
            long total=page.getTotal();
            System.out.println(total);
            LocalPageUtil.set(PageInfo.builder().Total(total).build());
        }

        return select;
    }

    public Integer insert(Product product){
        return   productDao.insert(product);
    }
    public Integer update(Product product){
        return   productDao.update(product);
    }
    public Integer delete(Integer id){
        return   productDao.updateStatus(id,0);
    }

}
